package com.ge.modules.api.controller;

import com.ge.common.annotation.AuthIgnoreApi;
import com.ge.common.annotation.LoginTechnicianApi;
import com.ge.common.utils.DateUtils;
import com.ge.common.utils.PageUtils;
import com.ge.common.utils.Query;
import com.ge.common.utils.R;
import com.ge.modules.api.apiDTO.TbTechnicianDto;
import com.ge.modules.api.service.TbOrderService;
import com.ge.modules.api.service.TbStoreService;
import com.ge.modules.api.service.TbTechnicianService;
import com.ge.modules.entity.TbOrderEntity;
import com.ge.modules.entity.TbStoreEntity;
import com.ge.modules.entity.TbTechnicianEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * user端 技师信息接口
 * 
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-04-20 17:02:43
 */
@RestController
@RequestMapping("/api/user/technician")
public class TbUserTechnicianController {
	@Autowired
	private TbTechnicianService technicianService;

	@Autowired
	private TbStoreService tbStoreService;

	@Autowired
	private TbOrderService tbOrderService;
	
	/**
	 * 分页列表
	 */
	@AuthIgnoreApi
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<TbTechnicianEntity> technicianList = technicianService.queryList(query);
		int total = technicianService.queryTotal(query);
		List<TbTechnicianDto>  tbTechnicianDtos = new ArrayList<>();
		for (int i = 0; i < technicianList.size(); i++) {
			TbTechnicianDto tbTechnicianDto = technicianList.get(i).toDto();
			TbStoreEntity tbStoreEntity = tbStoreService.queryByStoreId(tbTechnicianDto.getStoreId());
			tbTechnicianDto.setTbStoreEntity(tbStoreEntity);
			//查询技师订单数
			Map<String, Object> map = new HashMap<>();
			map.put("commodityMasterId", tbTechnicianDto.getTechnicianId());
			//统计已完成的订单 5
//			map.put("orderStatus", 5);
			List<TbOrderEntity> tbOrderEntityList = tbOrderService.queryList(map);
			tbTechnicianDto.setOrderNumber(tbOrderEntityList.size());
			tbTechnicianDtos.add(tbTechnicianDto);
		}
		PageUtils pageUtil = new PageUtils(tbTechnicianDtos, total, query.getLimit(), query.getPage());
		return R.ok().put("data", pageUtil);
	}

	/**
     * 数据列表 不分页
	 * @param technician 技师对象
	 * @return
     */
	@AuthIgnoreApi
	@RequestMapping("/listInfo")
	public R listInfo(TbTechnicianEntity technician){
		List<TbTechnicianEntity> tbTechnicianEntityList = technicianService.queryListObject(technician);
		return R.ok().put("data", tbTechnicianEntityList);
	}


	/**
	 * 根据编码查询技师
	 * @param technicianId 技师id
	 * @return
	 */
	@RequestMapping("/info/{technicianId}")
	public R info(@PathVariable("technicianId") Integer technicianId){
		TbTechnicianEntity technician = technicianService.queryByTechnicianId(technicianId);
		return R.ok().put("data", technician);
	}


}
